package com.jnxx.robot.controller;

import com.jnxx.basic.util.HttpTool;
import com.jnxx.robot.domain.Robot;
import com.jnxx.robot.mapper.RobotMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  机器人群通知
 */
@RestController
@RequestMapping("/robot")
public class RobotNotification {

    @Autowired
    private RobotMapper robotMapper;

    /**
     * 普通发消息接口
     * @param content
     * @return
     */
    @PostMapping("/sendRobotMessage")
    public String sendRobotMessage(@RequestBody Map<String, String> content){
        String textMessage = content.get("text");
        String url = content.get("url");
        try {
//            String url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=f5eb4e7f-e602-497a-b403-d080b55854a8";

            Map<String, Object> textcard = new HashMap<>();
//            textcard.put("nowTime", nowTime);
            textcard.put("content", textMessage);
            textcard.put("mentioned_list", "@all");

            Map<String, Object> sendMessageMap = new HashMap<>();
            sendMessageMap.put("msgtype", "text");
            sendMessageMap.put("text", textcard);
            return  HttpTool.sendPost22(url, sendMessageMap);
        } catch (Exception e) {
            e.printStackTrace();
            return "请求失败";
        }
    }

    // 工作日每天早上9点触发
    @Scheduled(cron = "0 0 9 * * MON-FRI")
//    @PostMapping("/time")
    public void morningTaskWeekday() {
        List<Robot> robotList = robotMapper.findOpenList("open");
        if (robotList.size() > 0){
            for (Robot robot : robotList) {
                HashMap<String, String> map = new HashMap<>();
                map.put("url", robot.getUrl());
                map.put("text", robot.getMorningInformation());
                sendRobotMessage(map);
            }
        }

//        String url = "http:47.110.49.47:8089/api/sendRobotMessage";
//        HashMap<String, String> map = new HashMap<>();
//        String s = HttpTool.sendPost11(url, map);
//        JSONObject jsonObject = JSONObject.parseObject(s);
//        String status = jsonObject.getString("status");
//        String data = jsonObject.getString("data");

        System.out.println("早上9点定时任务执行：" );
        // 这里是你早上9点定时任务的具体逻辑
    }

    // 工作日每天晚上9点触发
    @Scheduled(cron = "0 0 21 * * MON-FRI")
    public void eveningTaskWeekday() {
        List<Robot> robotList = robotMapper.findOpenList("open");
        if (robotList.size() > 0){
            for (Robot robot : robotList) {
                HashMap<String, String> map = new HashMap<>();
                map.put("url", robot.getUrl());
                map.put("text", robot.getNightInformation());
                sendRobotMessage(map);
            }
        }
        System.out.println("晚上9点定时任务执行：" );
        // 这里是你晚上9点定时任务的具体逻辑
    }


}
